/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-05-24 11:15:38
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-24 11:41:34
 * @FilePath: /consult-patient-vue3-ts/src/composable/index.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { OrderType } from '@/enums'
import { followOrUnfollow } from '@/services/consult'
import { ref } from 'vue'
import type { FollowType } from 'vue'
import { getPrescriptionPic,deleteOrder } from '@/services/consult'
import { onShowPrescription,showImagePreview,showSuccessToast,showFailToast } from 'vant'

// 封装公共关注/取消关注逻辑
export const useFollow = (type: FollowType) => {
  const loading = ref(false)
  const follow = async (item:{id:string,likeFlag:0|1})=>{
    loading.value = true
    try{
        await followOrUnfollow(item.id,type)
        item.likeFlag = item.likeFlag === 1 ? 0 : 1
    }finally{
        loading.value = false
    }
  }
  return {
    loading,
    follow
  }
}
// 封装查看处方逻辑
export const useShowPrescription = () => {
  const onShowPrescription = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return { onShowPrescription }
}
// 删除订单
export const useDeleteOrder = (cb: () => void) => {
  const loading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      cb && cb()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, deleteConsultOrder }
}